Skip to content

Conversation

@deepika-awasthi
Copy link

Add multiline logging interceptor sample for Datadog compatibility

Summary

Adds a new sample demonstrating how to format multiline exception logs as single-line JSON within Temporal SDK boundaries. This solves the issue where multiline tracebacks span multiple log entries in log aggregation systems like Datadog, making them difficult to parse and analyze.

The solution implements a surgical interceptor that:

  • Captures exceptions in both activities and workflows
  • Formats exception details as single-line JSON with message, type, and traceback fields
  • Replaces newlines in tracebacks with " | " separators for readability
  • Logs the formatted exception and re-raises the original to preserve normal error handling
  • Only affects logging within Temporal SDK boundaries without interfering with global formatters

The implementation follows the established interceptor pattern from the sentry sample.

Review & Testing Checklist for Human

  • End-to-end integration testing: Run the sample with actual Temporal server (temporal server start-dev) and verify multiline exceptions are logged as single-line JSON in worker output
  • Datadog compatibility verification: Confirm the JSON format actually solves the original multiline parsing issue in Datadog (test with actual Datadog agent if possible)
  • Exception handling correctness: Verify that original exceptions are properly preserved and re-raised, maintaining normal error handling behavior
  • Workflow sandbox usage review: Ensure the workflow.unsafe.sandbox_unrestricted() usage in the workflow interceptor is appropriate and follows Temporal best practices
  • Interceptor pattern compliance: Review that the implementation correctly follows Temporal SDK interceptor patterns and doesn't introduce any subtle bugs

Notes

Testing limitations: The implementation wasn't fully tested in a live Temporal environment due to temporal CLI not being available in the development environment. The core JSON formatting logic was validated, but full interceptor integration testing is needed.

Performance impact: The interceptor adds JSON serialization overhead on every exception, but this should be minimal since exceptions are rare in normal operation.

Requested by: @deepika-awasthi
Link to Devin run: https://app.devin.ai/sessions/37122594b322437b8e42cef51d4104d1

- Implements surgical solution for formatting multiline exception logs as single-line JSON
- Only affects logging within Temporal SDK boundaries, doesn't interfere with global formatters
- Captures exceptions in both activities and workflows using interceptor pattern
- Formats exceptions as JSON with message, type, and traceback fields
- Includes comprehensive sample with activities, workflows, worker, and starter
- Follows established interceptor patterns from sentry sample
- Solves Datadog multiline log parsing issues for Temporal exceptions

Requested by: @deepika-awasthi
Link to Devin run: https://app.devin.ai/sessions/37122594b322437b8e42cef51d4104d1

Co-Authored-By: deepika awasthi <deepika.awasthi@temporal.io>
@deepika-awasthi deepika-awasthi requested a review from a team as a code owner September 23, 2025 20:28
devin-ai-integration bot and others added 3 commits September 23, 2025 20:33
…ckages

- Fixed import sorting in worker.py, starter.py, workflows.py, test_imports.py
- Added multiline_logging to pyproject.toml packages list for proper build inclusion
- All black formatting already applied in previous commit

Co-Authored-By: deepika awasthi <deepika.awasthi@temporal.io>
- Changed from absolute import 'from activities import' to relative import 'from .activities import'
- This resolves mypy errors: Module 'activities' has no attribute 'complex_failing_activity' and 'failing_activity'
- Follows the same import pattern used in workflows.py and other sample files
- All local lint checks now pass: black, isort, and mypy

Co-Authored-By: deepika awasthi <deepika.awasthi@temporal.io>
…for interceptor and workflows

- Changed 'from interceptor import' to 'from .interceptor import'
- Changed 'from workflows import' to 'from .workflows import'
- This resolves pytest collection error: ModuleNotFoundError: No module named 'interceptor'
- All imports in test_imports.py now use relative imports consistently
- Local lint and test checks pass completely

Co-Authored-By: deepika awasthi <deepika.awasthi@temporal.io>
@deepika-awasthi
Copy link
Author

not needed

@deepika-awasthi deepika-awasthi deleted the devin/1727124409-multiline-logging-interceptor branch September 23, 2025 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants